package com.hzhr.iot.cqgas.ds.service;

import com.hzhr.iot.core.constant.CommonOptions;
import com.hzhr.iot.cqgas.ds.dao.device.example.DevProdFactoryDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.example.EneWuziChangshangDao;
import com.hzhr.iot.cqgas.ds.dao.user.example.UsrCompanyDao;
import com.hzhr.iot.cqgas.ds.entity.device.DevProdFactoryEntity;
import com.hzhr.iot.cqgas.ds.entity.device.DevProdFactoryExample;
import com.hzhr.iot.cqgas.ds.entity.smartmeter.EneWuziChangshangEntity;
import com.hzhr.iot.cqgas.ds.entity.user.UsrCompanyEntity;
import com.hzhr.iot.cqgas.ds.entity.user.UsrCompanyExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProdFactoryService {
    @Autowired
    DevProdFactoryDao devProdFactoryDao;
    @Autowired
    EneWuziChangshangDao eneWuziChangshangDao;
    @Autowired
    UsrOptionsService usrOptionsService;
    @Autowired
    UsrCompanyDao usrCompanyDao;

    @Value("${companyId}")
    String companyId;

    private Logger logger = LoggerFactory.getLogger(ProdFactoryService.class);

    public void syncProdFactory() {
        int addCount = 0;
        int updateCount = 0;
        long start = new Date().getTime();

        List<EneWuziChangshangEntity> eneWuziChangshangEntities = eneWuziChangshangDao.selectByExample(null);
        for (EneWuziChangshangEntity eneWuziChangshangEntity :
        eneWuziChangshangEntities) {
            try {
                String id = eneWuziChangshangEntity.getId();
                String des = eneWuziChangshangEntity.getDes();

                UsrCompanyExample usrCompanyExample = new UsrCompanyExample();
                usrCompanyExample.createCriteria().andOutIdEqualTo(id);
                List<UsrCompanyEntity> usrCompanyEntities = usrCompanyDao.selectByExample(usrCompanyExample);
                Integer iotCompnayId = null;
                if (usrCompanyEntities.size() > 0) {
                    iotCompnayId = usrCompanyEntities.get(0).getId();
                }

                DevProdFactoryExample devProdFactoryExample = new DevProdFactoryExample();
                devProdFactoryExample.createCriteria().andOutIdEqualTo(id);
                List<DevProdFactoryEntity> devProdFactoryEntities = devProdFactoryDao.selectByExample(devProdFactoryExample);

                if (devProdFactoryEntities.size() > 0) {
                    DevProdFactoryEntity devProdFactoryEntity = devProdFactoryEntities.get(0);
                    devProdFactoryEntity.setDes(des);
                    devProdFactoryEntity.setCompanyId(Integer.parseInt(companyId));
                    devProdFactoryEntity.setUpdateTime(new Date());
                    devProdFactoryEntity.setUpdateUserId(0);
                    devProdFactoryEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devProdFactoryEntity.setState("1");
                    if (iotCompnayId != null) {
                        devProdFactoryEntity.setIotCompanyId(iotCompnayId);
                    }
                    devProdFactoryDao.updateByPrimaryKey(devProdFactoryEntity);
                    updateCount++;
                } else {
                    DevProdFactoryEntity devProdFactoryEntity = new DevProdFactoryEntity();
                    devProdFactoryEntity.setOutId(id);
                    devProdFactoryEntity.setCompanyId(Integer.parseInt(companyId));
                    devProdFactoryEntity.setDes(des);
                    devProdFactoryEntity.setCreateTime(new Date());
                    devProdFactoryEntity.setCreateUserId(0);
                    devProdFactoryEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devProdFactoryEntity.setState("1");
                    if (iotCompnayId != null) {
                        devProdFactoryEntity.setIotCompanyId(iotCompnayId);
                    }
                    devProdFactoryDao.insert(devProdFactoryEntity);
                    addCount++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        long end = new Date().getTime();
        logger.info("同步生产厂家结束: " + (end - start) / 1000 + "s");
        logger.info("同步时间: " + (end - start) / 1000 + "s");
        logger.info("更新数量: " + updateCount + "条");
        logger.info("新增数量: " + addCount + "条");
    }
}
